package com.bkhc.product.service;

import com.bkhc.common.util.MyBeanUtils;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.product.entity.Spec;
import com.bkhc.product.entity.SpecRange;
import com.bkhc.product.repository.SpecRangeRepository;
import com.bkhc.product.repository.SpecRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecService {

    @Autowired
    private SpecRepository repository;

    @Autowired
    private SpecRangeRepository specRangeRepository;

    public ResultJson<?> querySpecs(Spec spec, int pageIndex, int pageSize){
        Sort.Order orders = new Sort.Order(Sort.Direction.DESC, "updateDate");
        PageRequest pageRequest = PageRequest.of(pageIndex-1, pageSize, Sort.by(orders));
        Page<Spec> page = null;
        if(spec == null){
            page = repository.findAll(pageRequest);
        }else {
            page = repository.findAll(spec, pageRequest);
        }
        PageUtil pageUtil = new PageUtil(page);
        ResultJson<PageUtil> resultJson = ResultJson.success();
        resultJson.setData(pageUtil);
        System.out.println(pageUtil.getTotal());
        return  resultJson;

    }

    public ResultJson<?> querySpecsDetail(Spec spec){
        Spec res = repository.findBySpecCode(spec.getSpecCode());
        List<SpecRange> ranges = specRangeRepository.findBySpecCode(spec.getSpecCode());
        res.setRanges(ranges);
        ResultJson<Spec> resultJson = ResultJson.success();
        resultJson.setData(res);
        return  resultJson;

    }

    public ResultJson<?> addSpecs(Spec spec) {
        spec.setStatus("1");
        Spec o = repository.findBySpecName(spec.getSpecName());
        if(o!=null){
            ResultJson<Page<Spec>> resultJson = ResultJson.failure(ResultCode.ISEXIT_RESURCE);
            return resultJson;
        }
        repository.save(spec);
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<?> updateSpec(Spec spec) {
        Spec oldspec = repository.findBySpecCode(spec.getSpecCode());
        MyBeanUtils.copyProperties(spec,oldspec);
        repository.save(spec);
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<?> deleteSpec(Spec spec) {
        repository.deleteBySpecCode(spec.getSpecCode());
        spec.getRanges();
        specRangeRepository.deleteBySpecCode(spec.getSpecCode());
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<?> addSpecRange(SpecRange range) {
        range.setStatus("1");
        SpecRange o = specRangeRepository.findByRangeValueAndSpecCode(range.getRangeValue(),range.getSpecCode());
        if(o!=null){
            ResultJson<Page<Spec>> resultJson = ResultJson.failure(ResultCode.ISEXIT_RESURCE);
            return resultJson;
        }
        specRangeRepository.save(range);
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<?> deleteSpecRange(SpecRange range) {
        specRangeRepository.deleteByRangeCode(range.getRangeCode());
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<List<SpecRange>> querySpecRange(SpecRange range) {
        List<SpecRange> specRangs = specRangeRepository.findBySpecCode(range.getSpecCode());
        ResultJson<List<SpecRange>> resultJson = ResultJson.success();
        resultJson.setData(specRangs);
        return  resultJson;
    }

    public ResultJson<?> updateRangValue(SpecRange range) {
        SpecRange  tem=specRangeRepository.findByRangeCode(range.getRangeCode());
        tem.setStatus(range.getStatus());
        specRangeRepository.save(tem);
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }

    public ResultJson<?> updateSpcStatus(Spec spec) {
        Spec  tem=repository.findBySpecCode(spec.getSpecCode());
        tem.setStatus(spec.getStatus());
        repository.save(tem);
        ResultJson<Page<Spec>> resultJson = ResultJson.success();
        return  resultJson;
    }
}
